#include "TcpServerManage.h"

TcpServerManage::TcpServerManage(const int &handleMaxThreadCount, QObject * parent) :
AbstractManage(handleMaxThreadCount, parent)
{ 

}

TcpServerManage::~TcpServerManage()
{
	if (this->isRunning())
	{
		this->close();
	}
}

bool TcpServerManage::listen(const QHostAddress &address, const quint16 &port)
{
	listenAddress_ = address;
	listenPort_ = port;

	return this->begin();
}

bool TcpServerManage::isRunning()
{
	return !tcpServer_.isNull();
}

bool TcpServerManage::onStart()
{
	mutex_.lock();

	tcpServer_ = new QTcpServer;

	mutex_.unlock();

	QObject::connect(tcpServer_.data(), &QTcpServer::newConnection, [this]()
	{
		this->newSession(new Session(this->tcpServer_->nextPendingConnection()));
	});

	if (!tcpServer_->listen(listenAddress_, listenPort_))
	{
		mutex_.lock();

		delete tcpServer_.data();
		tcpServer_.clear();

		mutex_.unlock();

		return false;
	}

	return true;
}

void TcpServerManage::onFinish()
{
	this->mutex_.lock();

	tcpServer_->close();
	delete tcpServer_.data();
	tcpServer_.clear();

	this->mutex_.unlock();
}
